package com.feshfans.bridge;

import com.feshfans.controller.RegisteredController;
import com.feshfans.controller.StageManager;
import com.feshfans.controller.special.RoomController;
import javafx.application.Platform;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.muc.Affiliate;
import org.jivesoftware.smackx.muc.MUCAffiliation;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class RoomBridge extends AbstractBridge {

    private static final Logger Log = LoggerFactory.getLogger(RoomBridge.class);
    private static final RoomBridge ROOM_BRIDGE = new RoomBridge();

    private Map<EntityBareJid,RoomController> roomControllerMap = new HashMap<>();

    private RoomBridge(){}

    public static RoomBridge getInstance(){
        return ROOM_BRIDGE;
    }


    @Override
    public synchronized void register(RegisteredController controller) {
        RoomController roomController = (RoomController) controller;
        if(!roomControllerMap.containsKey(roomController.getRoomJid())){
            roomControllerMap.put(roomController.getRoomJid(),roomController);
        }
    }

    @Override
    public synchronized void unregister(RegisteredController controller) {
        RoomController roomController = (RoomController) controller;
        roomControllerMap.remove(roomController.getRoomJid());
    }

    public synchronized void processMessage(Message message){

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                // 获取消息来源
                Jid jid = message.getFrom();
                // 获取资源名称，群聊中每个人是当成 RoomJid 的一种资源在其中的
                String nickName = jid.getResourceOrEmpty().toString();
                EntityBareJid roomJid = null;
                try {
                    // 生成房间的 roomJid
                    roomJid = JidCreate.entityBareFrom(message.getFrom());
                    // 如果不包含，说明未打开，需要打开窗口
                    if(!roomControllerMap.containsKey(roomJid)){
                        StageManager.openOrGetRoomStage(roomJid);
                    }
                    RoomController roomController = roomControllerMap.get(roomJid);
                    roomController.processMessage(nickName,message.getBody());
                } catch (XmppStringprepException e) {
                    Log.error("jid:{},message:{}",jid,message);
                }


            }
        });
    }

    public synchronized void processPresence(Jid roomJid, Jid fromJid, Resourcepart nickName, MUCAffiliation affiliate){

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                // 生成房间的 roomJid
                try {
                    EntityBareJid entityBareJid = JidCreate.entityBareFrom(roomJid);
                    // 如果不包含，说明未打开，需要打开窗口
                    if(!roomControllerMap.containsKey(entityBareJid)){
                        StageManager.openOrGetRoomStage(entityBareJid);
                    }
                    RoomController roomController = roomControllerMap.get(entityBareJid);
                    roomController.processPrecense(fromJid,nickName.toString(),affiliate.name());
                } catch (XmppStringprepException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
